package com.samoiloff.ui.btns.tab {
	import Boolean;
	import int;
	import com.samoiloff.ui.btns.tab.TabBtnController;
	import com.samoiloff.util.data.ArrItemDispatcher;

	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author Samoiloff (http://blog.samoiloff.com)
	 */
	public class TabsBar extends ArrItemDispatcher {
		
		private var _selectedIndex: int;
		private var _enabled: Boolean;

		public function TabsBar() {
			_selectedIndex = -1;
		}

		public function addBtn(tabBtn:TabBtnController):void {
			tabBtn.addEventListener(MouseEvent.CLICK, onClick);
			_arr.push(tabBtn);
			updateBtns();
		}

		private function onClick(e:MouseEvent): void {
			var tabBtn:TabBtnController = TabBtnController(e.target);
			selectedIndex = getIndexOf(tabBtn);
		}
		
		public function getItemAt(i:int):TabBtnController {
			return TabBtnController(_arr[i]);
		}
		
		public function get selectedIndex(): int {
			return _selectedIndex;
		}
		
		public function set selectedIndex(selectedIndex: int): void {
			if (_selectedIndex != selectedIndex) {
				_selectedIndex = selectedIndex;
				updateBtns();
				dispatchEvent(new Event(Event.CHANGE));
			}
		}
		
		private function updateBtns():void {
			for (var i:int = 0; i<length; i++) {
				getItemAt(i).selected = i == _selectedIndex;
			}
		}
		
		public function get enabled(): Boolean {
			return _enabled;
		}
		
		public function set enabled(enabled: Boolean): void {
			_enabled = enabled;
			for (var i:int = 0; i<length; i++) {
				getItemAt(i).enabled = _enabled;
			}
		}
	}
}
